package com.pig4cloud.pigx.contract.executer.rate_settlement.job;

import com.alibaba.fastjson.JSONObject;
import com.pig4cloud.pigx.common.customize.exception.MqAckException;
import com.pig4cloud.pigx.contract.bo.RateSettlementJobBO;
import com.pig4cloud.pigx.contract.constant.ContractMqConstants;
import com.pig4cloud.pigx.contract.executer.rate_settlement.job.canceler.RateSettlementJobCanceler;
import com.pig4cloud.pigx.contract.mongo.entity.ContractKLineTradeCacheMongo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 资金费率任务 执行器
 */
@Slf4j
@Component
@AllArgsConstructor
public class RateSettlementJobExecutor {

	private final RateSettlementJobCanceler canceler;

	/**
	 * 监听 contract.init.trade 队列
	 *
	 * @since 2022/9/8 15:44:00
	 */
	@RabbitListener(bindings = {
			@QueueBinding(
					value = @Queue(value = ContractMqConstants.QUEUE_FANOUT_RATE_SETTLEMENT_JOB),
					exchange = @Exchange(value = ContractMqConstants.EXCHANGE_FANOUT_RATE_SETTLEMENT_JOB, type = ExchangeTypes.FANOUT)
			)})
	public void expire(String obj) {
		RateSettlementJobBO bo = JSONObject.parseObject(obj, RateSettlementJobBO.class);
		try {
			if (bo.getIsSave()) {
				canceler.add(bo);
				log.error("开始处理资金费率扣除任务");
			} else {
				canceler.remove(bo);
				log.error("开始处理资金费率增加任务");
			}
		} catch (MqAckException ignore) {
		}
	}

}
